/* ************************************************************************

   Copyright:

   License:

   Authors:

************************************************************************ */

/* ************************************************************************

#asset(mmclient/*)

************************************************************************ */

/**
 * The GUI definition of the Map Medley client.
 */
qx.Class.define("mmclient.Login",
{
  extend : qx.ui.container.Composite,


  /*
  *****************************************************************************
     CONSTRUCTOR
  *****************************************************************************
  */

  construct : function()
  {
    this.base(arguments);

    // Configure layout
    var layout = new qx.ui.layout.VBox;
    layout.setSeparator("separator-vertical");
    this.setLayout(layout);

    // Header
    this.add(this._createHeader());
  },


/*
  *****************************************************************************
     MEMBERS
  *****************************************************************************
  */

  members :
  {
    
    /**
     * Creates the application header.
     */
    _createHeader : function()
    {
      var layout = new qx.ui.layout.HBox();
      var header = new qx.ui.container.Composite(layout);
      header.setBackgroundColor("#CC0000");
      header.setTextColor("#FFFFFF");

      var title = new qx.ui.basic.Label("Map Medley Client version 0.1.999.0");
      var btn = new qx.ui.form.Button("Login");

      btn.addListener("execute", function(e) {
        this.createLogin();
      }, this);

      header.add(title);
      header.add(new qx.ui.core.Spacer, {flex : 1});
      header.add(btn);

      return header;
    },

    
    /**
     * Creates a sample login widget
     */
    createLogin : function()
    {
      var loginWidget = new mmclient.dialog.Login({
        image       : "mmclient/login_logo.png", 
        text        : "Map Medley Client login:",
        callback    : this.loginCallback,
        context     : this
      });
      loginWidget.addListener("loginSuccess", function(e){
        qx.core.Init.getApplication().getRoot().removeAll();
        if (qx.core.Init.getApplication().getAppHistory().getState() == "" ||
            qx.core.Init.getApplication().getAppHistory().getState() == "undefined")
        {
          window.location = "http://localhost/index.php";
        }

        // Initialize the viewer
        qx.core.Init.getApplication().viewer = new mmclient.MapMedley;
        qx.core.Init.getApplication().getRoot().add(qx.core.Init.getApplication().viewer, {edge:0});
        qx.core.Init.getApplication().viewer._viewMain.initialize();
      }, this);
      loginWidget.addListener("loginFailure", function(e){
        mmclient.dialog.Dialog.error(e.getData());
      }, this);
      loginWidget.show();
    },
    
    /**
    * Sample callback function that takes the username, password and
    * another callback function as parameters. The passed function
    * is called with a boolean value (true=authenticated, false=
    * authentication failed) and a string value which contains 
    * an error message like so: 
    * callback.call( context, {Boolean} result, {String} message);
    * @param username {String}
    * @param password {String}
    * @param callback {Function} The callback function
    * @param context {Object} The execution context
    */    
   loginCallback : function( username, password, callback, context )
   {
      var rpc = new qx.io.remote.Rpc("http://localhost/services/", "qooxdoo.mmserv");
      rpc.setCrossDomain(true);
      rpc.setTimeout(10000);

      var login_callback = function(result, exc)
      {
        if (exc == null)
        {
          if (result == true)
          {
            qx.core.Init.getApplication().setUsername(username);
            callback.call(context, true);
          }
          else
          {
            callback.call(context, false, "Login incorrect.");
          }
        }
        else
        {
          alert("Exception during async call: " + exc);
        }
      };

      rpc.callAsync(login_callback, "login",
      {
        user  : username,
        pass : password
      });
    },
    
    /**
     * Initialize
     */
    initialize : function()
    {
      var rpc = new qx.io.remote.Rpc("http://localhost/services/", "qooxdoo.mmserv");
      rpc.setCrossDomain(true);
      rpc.setTimeout(10000);

      var self = this;
      var isLoggedIn_callback = function(result, exc)
      {
        if (exc == null)
        {
          if (result != "")
          {
            qx.core.Init.getApplication().getRoot().removeAll();
            qx.core.Init.getApplication().setUsername(result);
            if (qx.core.Init.getApplication().getAppHistory().getState() == "" ||
                qx.core.Init.getApplication().getAppHistory().getState() == "undefined")
            {
              window.location = "http://localhost/login/main.php";
            }

            // Initialize the viewer
            qx.core.Init.getApplication().viewer = new mmclient.MapMedley;
            qx.core.Init.getApplication().getRoot().add(qx.core.Init.getApplication().viewer, {edge:0});
            qx.core.Init.getApplication().viewer._viewMain.initialize(qx.core.Init.getApplication().getAppHistory().getState());
          }
          else
          {
            self.createLogin();
          }
        }
        else
        {
          alert("Exception during async call: " + exc);
        }
      };

      rpc.callAsync(isLoggedIn_callback, "isLoggedIn");
    }
  },


  /*
  *****************************************************************************
     DESTRUCTOR
  *****************************************************************************
  */

  destruct : function()
  {
    //this.widgets = this.tests = this._sampleToTreeNodeMap = this.tree = this.logelem = null;
    //this._disposeObjects();
  }
});
